package org.fuys.coder.domain.file.service;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.auth.sts.AssumeRoleRequest;
import com.aliyuncs.auth.sts.AssumeRoleResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import org.fuys.coder.common.config.AliYunOssConfig;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

/**
 * @projectName: fuys-low-coder
 * @package: org.fuys.coder.domain.file.service
 * @className: AliYunStsTokenService
 * @author: WangJing
 * @description: 获取阿里云上传token
 * @date: 2024/6/20 19:59
 * @version: 1.0
 */
@Service
public class AliYunStsTokenService {

    @Resource
    private AliYunOssConfig aliYunOssConfig;

    private IAcsClient client;
    @PostConstruct
    public void initClient(){
        client=new DefaultAcsClient(
                DefaultProfile.getProfile(aliYunOssConfig.getRegion(), aliYunOssConfig.getAccessKeyId()
                        , aliYunOssConfig.getAccessKeySecret())
        );
    }


    public AssumeRoleResponse getStsToken() throws ClientException {
        AssumeRoleRequest request=new AssumeRoleRequest();
        request.setRoleArn(aliYunOssConfig.getRoleArn());
        request.setRoleSessionName(aliYunOssConfig.getRoleSessionName());
        return client.getAcsResponse(request);
    }
}
